<?php
	class Question_model extends MY_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		
		/**
		* get_question(): 	get the records of Question with LIMIT	
		* 
		* @param 	int $kid - the id of Question
		*			int $g_id - the id of Game
		*
		* @return    array - Collect data of query
		* @access public
		*/
		public function get_question($id=FALSE, $g_id=FALSE){
			if($id === FALSE && $g_id === FALSE){
				$this->db->select('q.*, g.title');
				$this->db->from('question q');
				$this->db->join('trivia_game g', 'g.id = q.trivia_game_id', 'left');
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();				
				return $query->result_array();
			}
			elseif($id===FALSE && $g_id != FALSE){			
				$this->db->select('q.*, g.title');
				$this->db->from('question q');
				$this->db->join('trivia_game g', 'g.id = q.trivia_game_id', 'left');
				$this->db->where('q.trivia_game_id = '. $g_id, NULL, FALSE);
				$this->db->limit($this->record_per_page, $this->limit);
				
				$query = $this->db->get();
				return $query->result_array();
			}
			else{
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get_where('question', array('question.id' => $id));
				return $query->row_array();
			}
		}		
		
		
		/**
		* get_question_list(): 	get the records of Question without LIMIT	
		* 
		* @param 	int $g_id - the id of Game
		*
		* @return    array - Collect data of query
		* @access public
		*/
		public function get_question_list($g_id=FALSE){
			if($g_id!=FALSE)
				$query = $this->db->get_where('question', array('trivia_game_id' => $g_id));
			else
				$query = $this->db->get('question');
			return $query->result_array();
		}
		
		
		/**
		* get_total(): 	get the number of total record of question	
		* 
		* @param 	
		*
		* @return    int the number of record
		* @access public
		*/
		public function get_total(){
			return count($this->get_question_list());
		}
		
		
		/**
		* get_total_search(): 	get the number of total record of searching query question	
		* 
		* @param 	String $src, search string
		*
		* @return    int the number of record
		* @access public
		*/
		public function get_total_search($src=FALSE){
			$this->db->select('q.*, g.title');
			$this->db->from('question q');
			$this->db->join('trivia_game g', 'g.id = q.trivia_game_id', 'left');
			$this->db->like('question', $src);				
			$query = $this->db->get();				
			return $query->num_rows();
		}
		
		
		/**
		* get_total_filter(): 	get the number of total record of filter query question	
		* 
		* @param 	String $src, search string
		*
		* @return    int the number of record
		* @access public
		*/
		public function get_total_filter($g_id=FALSE){
			$this->db->select('q.*, g.title');
			$this->db->from('question q');
			$this->db->join('trivia_game g', 'g.id = q.trivia_game_id', 'left');
			$this->db->where('q.trivia_game_id = '. $g_id, NULL, FALSE);			
			$query = $this->db->get();
			return $query->num_rows();
		}
		
		
		/**
		* search(): 	search 	the question
		* 
		* @param 	String $src, search string
		*
		* @return    array - collect data of question
		* @access public
		*/
		public function search($src=FALSE){
			$this->db->select('q.*, g.title');
			$this->db->from('question q');
			$this->db->join('trivia_game g', 'g.id = q.trivia_game_id', 'left');
			$this->db->like('q.question', $src);		
			$this->db->limit($this->record_per_page, $this->limit);
			$query = $this->db->get();				
			return $query->result_array();
		}
		
		
		/**
		* save(): 	save the data the question
		* 
		* @param 	
		*
		* @return    int - the reuslt of query
		* @access public
		*/
		public function save(){
			$id 	= $this->input->post('id');
			
			$data = array(
				"question" 			=> $this->input->post('question'),	
				"trivia_game_id"	=> $this->input->post('trivia_game_id')
			);
			if(empty($id))
				return $this->db->insert('question', $data);
			else{
				$this->db->where('id', $id);
				return $this->db->update('question', $data);
			}
		}
		
		
		/**
		* delete(): 	delete the data the question
		*				check the reference record in answer table
		* 
		* @param 		int - the id of question
		*
		* @return    
		* @access public
		*/
		public function delete($id){
			$sql = "SELECT `id` FROM `answer` WHERE `question_id` = $id";
			$query = $this->db->query($sql);
			if($query->num_rows()==0){
				$this->db->where('id', $id);
				$this->db->delete('question');
			}
		}		
	}
?>